<?php
header('Content-Type: text/html; charset=utf-8');
include('../class/class_connect.php');
include('../includes/rubriks.php');
include('../includes/transliterate.php');
include('../class/class_checkGetData.php');
if(!function_exists('mb_ucfirst')) {
    function mb_ucfirst($str, $enc = 'utf-8') {
    		return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
    }
}
$aCheck=new checkGetData('f');
foreach($_GET as $key=>$vol)
{
	$_GET[$key]=$aCheck->safetyCheck($vol);
}
if(!$_GET['city'])exit;
$aCheck->checkCity($_GET['city']);
$result=array();
$value=$_GET['search'];
iconv("UTF-8", "WINDOWS-1251", $value);
//echo mb_ucfirst($value);
if(strlen($value)>0)
{
	//echo $value.'<br>';
    foreach($divises as $division=>$vol)
    {
    	if(!empty($value))
		{//echo $division.'-'.$value.'<br>';
    	if(mb_stripos(mb_strtolower($division, 'utf-8'), mb_strtolower($value, 'utf-8'))===0)// || mb_strripos($division, $value))
    	{
			//echo $division;
    		$result[]=$division;
    	}
    	for($i=0; $i<count($vol); $i++)
    	{
    		$subdiv=$vol[$i];
    		if(mb_stripos(mb_strtolower($subdiv, 'utf-8'), mb_strtolower($value,'utf-8'))===0)
    		{
    			//echo $subdiv;
    			$result[]=$subdiv;
    		}
    	}
    	}
    }
	for($n=0; $n<4; $n++)
	{
		if($n==1)$value=mb_ucfirst($value);
		if($n==3)$value=mb_strtoupper($value, 'utf-8');
		if($n==2)$value=mb_transliterate($value);

		$aConnect=new connect('f');
		$res=$aConnect->select_all($_GET['city'], "WHERE name LIKE '".$value."%' or short LIKE '%".$value."%'");
		while($row=mysql_fetch_array($res))
		{
			if(!empty($row['name']) && !empty($value))
			{
			if(mb_stripos($row['name'], $value)===0 && strlen($row['name'])>0)
			{
				if(!in_array($row['name'], $result))
				{
					//echo $row['name'];
					$result[]=$row['name'];
				}
			}
			}
			if(!empty($row['short']) && !empty($value))
			{
			if(mb_stripos($row['short'], $value)===0)
			{
				if(!in_array($row['short'], $result))
				{
					//echo $row['short'];
					$result[]=$row['short'];
				}
			}
			}
		}
		$aConnect=new connect('m');
		$res=$aConnect->select_all($_GET['city'], "WHERE name LIKE '".$value."%' or short LIKE '%".$value."%'");
		while($row=mysql_fetch_array($res))
		{
			if(!empty($row['name']) && !empty($value))
			{
			if(mb_stripos($row['name'], $value)===0)
			{
				if(!in_array($row['name'], $result))
				{
					//echo $row['name'];
					$result[]=$row['name'];
				}
			}
			}
			if(!empty($row['short']) && !empty($value))
			{
			if(mb_stripos($row['short'], $value)===0)
			{
				if(!in_array($row['short'], $result))
				{
					//echo $row['short'];
					$result[]=$row['short'];
				}
			}
			}
		}
		$aConnect=new connect('pr');
		$res=$aConnect->select_all($_GET['city'], "WHERE name LIKE '".$value."%' or brend LIKE '%".$value."%' or model LIKE '%".$value."%'");
		//$i=0;
		while($row=mysql_fetch_array($res))
		{
			if(!empty($row['name']) && !empty($value))
			{
			if(mb_stripos($row['name'], $value)===0)
			{
				if(!in_array($row['name'], $result))
				{
					//echo $row['name'];
					$result[]=$row['name'];
				}
			}
			}
			if(!empty($row['brend']) && !empty($value))
			{
			if(mb_stripos($row['brend'], $value)===0)
			{
				if(!in_array($row['brend'], $result))
				{
					//echo $row['brend'];
					$result[]=$row['brend'];
				}
			}
			}
			if(!empty($row['model']) && !empty($value))
			{
			if(mb_stripos($row['model'], $value)===0)
			{
				if(!in_array($row['model'], $result))
				{
					//echo $row['model'];
					$result[]=$row['model'];
				}
			}
			}
			//if($i>10)break;
			//$i++;
		}
	}
	$result=array_unique($result);
	//sort($result);
	$i=0;
	foreach($result as $key=>$vol)
	{
		if($i>10)break;
		echo '<div class="suggestRes">'.$vol.'</div>';
		$i++;
	}
}

?>